package com.ruoyi.livedemo.mapper;

import com.ruoyi.livedemo.entity.StickyNote;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StickyNoteMapper {

    @Select("SELECT * FROM sticky_notes WHERE user_id=#{userId} ORDER BY pinned DESC, updated_at DESC")
    List<StickyNote> findByUser(Long userId);

    @Insert("INSERT INTO sticky_notes (user_id, title, content, color_hex, pinned, expires_at, cover_path) " +
            "VALUES (#{userId}, #{title}, #{content}, #{colorHex}, #{pinned}, #{expiresAt}, #{coverPath})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(StickyNote note);

    @Update("UPDATE sticky_notes SET title=#{title}, content=#{content}, color_hex=#{colorHex}, " +
            "pinned=#{pinned}, expires_at=#{expiresAt}, cover_path=#{coverPath} WHERE id=#{id} AND user_id=#{userId}")
    int update(StickyNote note);

    @Delete("DELETE FROM sticky_notes WHERE id=#{id} AND user_id=#{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT * FROM sticky_notes WHERE user_id=#{userId} ORDER BY pinned DESC, updated_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<StickyNote> findPage(@Param("userId") Long userId,
                              @Param("offset") int offset,
                              @Param("limit") int limit);

    @Select("SELECT COUNT(1) FROM sticky_notes WHERE user_id=#{userId}")
    long countByUser(Long userId);
}
